function get_list_data_filter($limit = 10, $offset = 0, $filter)
  	{
		if($offset==""){ $offset=0; }
	$query = $this->db->query("SELECT a.*, cabang.strnama_cabang FROM po a INNER JOIN cabang ON a.intid_cabang = cabang.intid_cabang 
	where cabang.strnama_cabang LIKE '%" . $filter['cari'] . "%' ORDER BY a.intid_po ASC  LIMIT $offset,$limit");
        return $query->result();
	}


	
    
	
	
      function getNoNota($id){
     if ($id)
	 {
	   $query = $this->db->query("select * from nota where intid_nota = $id and is_dp = 0");
	   return $query->result();
	 }
    }

	

        

    function getBarang(){

        $query = $this->db->query("SELECT DISTINCT a.*,b.strnama,c.strnama_cabang
                                    FROM po a
                                    RIGHT JOIN barang b ON a.intid_barang= b.intid_barang
                                    JOIN cabang c ON a.intid_cabang=c.intid_cabang
                                    ORDER BY a.intid_po ASC ");
	   return $query->result();
    }

    function get_po_by_id($id)
	{
		$this->db->select('*');
		$this->db->where('intid_po', $id);
		return $this->db->get('po');
	}


     
	
	 function insert_pusat($data){
         	$tgl = date("Y-m-d");
       
            $data = array(
            'intid_cabang' => $this->input->post('intid_cabang'),
			'intid_week' => $this->input->post('intid_week'),
            'datetgl' => $tgl
            );
        $this->db->insert('po_pusat', $data);
        return $this->db->insert_id();
	}
	
	
	
	
    
	
	function add_pusat($data)
	{
        $this->db->insert("po_pusat_detail", $data);
	}
	
	
	
	
	function add_surat_sttb($data)
	{
        $this->db->insert("sttb_detail", $data);
	}


       
	 function get_CetakPo_pusat($idPo) {
        $query = $this->db->query("SELECT DISTINCT po_pusat.intid_po, po_pusat.datetgl, po_pusat_detail.*,po_pusat.intid_cabang, cabang.strnama_cabang, barang.strnama, po_pusat.intid_week
FROM po_pusat
INNER JOIN po_pusat_detail ON po_pusat.intid_po = po_pusat_detail.intid_po
INNER JOIN barang ON po_pusat_detail.intid_barang = barang.intid_barang
INNER JOIN cabang ON po_pusat.intid_cabang = cabang.intid_cabang
                                    WHERE po_pusat.intid_po = '$idPo'");
	
        return $query->result();
     }

  	
	 function get_MaxSttb() {
        $this->db->select_max('intid_sttb');
		$query = $this->db->get('sttb');
        return $query;
     }
	 
	function get_MaxPoPusat() {
        $this->db->select_max('intid_po');
		$query = $this->db->get('po_pusat');
        return $query;
     }	 
//********************retur*****************//

      function insertRetur($data){
         $tgl = date("Y-m-d");

        // $week = intid_week ;
            $data = array(
            'intid_cabang' => $this->input->post('intid_cabang'),
            'datetgl' => $tgl,
            'ket' => $this->input->post('ket')
            );
        $this->db->insert('retur', $data);
        $intid_retur =$this->db->insert_id();
	}
        
     function get_retur_by_id($id)
	{
		$this->db->select('*');
		$this->db->where('intid_retur', $id);
		return $this->db->get('retur');
	}


        function addRetur($data)
	{
        $this->db->insert("retur_detail", $data);
	}

        function get_CetakRetur($idRetur) {
        $query = $this->db->query("SELECT DISTINCT retur.intid_retur, retur.datetgl,retur.ket, retur_detail.*,retur.intid_cabang, cabang.strnama_cabang, barang.strnama
FROM retur
INNER JOIN retur_detail ON retur.intid_retur = retur_detail.intid_retur
INNER JOIN barang ON retur_detail.intid_barang = barang.intid_barang
INNER JOIN cabang ON retur.intid_cabang = cabang.intid_cabang
                                    WHERE retur.intid_retur = '$idRetur'");

        return $query->result();
     }


	
  

	function delete($intid_po){
       $this->db->where('intid_po', $intid_po);
       $this->db->delete('po');
	   $this->db->where('intid_po', $intid_po);
       $this->db->delete('po_detail');
    }

	function select($intid_po){
       $query = $this->db->query("select * from po where intid_po= $intid_po");
	   return $query->result();
    }


     function nama_bulan($bulan) {
        switch ($bulan) {
            case '1':
                $b = "Januari";
                break;
            case '2':
                $b = "Februari";
                break;
            case '3':
                $b = "Maret";
                break;
            case '4':
                $b = "April";
                break;
            case '5':
                $b = "Mei";
                break;
            case '6':
                $b = "Juni";
                break;
            case '7':
                $b = "Juli";
                break;
            case '8':
                $b = "Agustus";
                break;
            case '9':
                $b = "September";
                break;
            case '10':
                $b = "Oktober";
                break;
            case '11':
                $b = "November";
                break;
            case '12':
                $b = "Desember";
                break;

        }

        return $b;
    }

    //surat jalan cabang

 function get_all_sjCab($data) {
        $this->db->select ('po.intid_po, po.datetgl, po_detail.*,po.intid_cabang, cabang.strnama_cabang, barang.strnama');
        $this->db->from('po');
        $this->db->join('po_detail', 'po_detail.intid_po = po.intid_po');
        $this->db->join('barang', 'barang.intid_barang = po_detail.intid_barang');
        $this->db->join('cabang', 'cabang.intid_cabang = po.intid_cabang');
      
        $where='';


        $this->db->where("MONTH(po.datetgl) = '$data[bulan]' and cabang.intid_cabang='$data[cabang]'");
   
        return $this->db->get()->result();
    }

   
	function get_list_datasj($limit = 10, $offset = 0)
  	{
            if($offset==""){ $offset=0; }
	$query = $this->db->query("SELECT DISTINCT a.*,b.strnama,c.strnama_cabang,d.intid_week
FROM po a
RIGHT JOIN barang b ON a.intid_barang= b.intid_barang
JOIN cabang c ON a.intid_cabang=c.intid_cabang
JOIN WEEK d ON a.intid_week = d.intid_week
ORDER BY a.intid_po ASC  LIMIT $offset,$limit");
		

        return $query->result();
	}

	function countDatasj()
	{
	  	return $this->db->count_all($this->tbl);
  	}

    function update_qtybarang($id_barang, $qty){
        $this->db->where('intid_barang', $id_barang);
		$this->db->update('barang', $qty);
    }
   
function get_list_sj()
  	{
           // if($offset==""){ $offset=0; }
	$query = $this->db->query("SELECT DISTINCT a.*,b.strnama,c.strnama_cabang,e.intquantity, f.strnama_jsatuan
FROM po a
INNER JOIN po_detail e ON a.intid_po = e.intid_po
RIGHT JOIN barang b ON e.intid_barang= b.intid_barang
JOIN cabang c ON a.intid_cabang=c.intid_cabang
INNER JOIN jenis_satuan f ON b.intid_jsatuan = f.intid_jsatuan
ORDER BY a.intid_po ASC ");
//	$query = $this->db->query("SELECT DISTINCT a.*,b.strnama,c.strnama_cabang,d.intid_week
//FROM po a
//RIGHT JOIN barang b ON a.intid_barang= b.intid_barang
//JOIN cabang c ON a.intid_cabang=c.intid_cabang
//JOIN WEEK d ON a.intid_week = d.intid_week
//ORDER BY a.intid_po ASC ");
        return $query->result();
	}

	  function search($key)
		{
        
        $query=$this->db->query("SELECT DISTINCT a.*,b.strnama,c.strnama_cabang,e.intquantity, f.strnama_jsatuan
FROM po a
INNER JOIN po_detail e ON a.intid_po = e.intid_po
RIGHT JOIN barang b ON e.intid_barang= b.intid_barang
JOIN cabang c ON a.intid_cabang=c.intid_cabang
INNER JOIN jenis_satuan f ON b.intid_jsatuan = f.intid_jsatuan
ORDER BY a.intid_po ASC
               where a.intid_unit = c.intid_unit
               and a.strnama_dealer LIKE '%$key%'");
	return $query->result();
	}
    	
	
	
	function get_list_data_detail_pusat($id)
  	{
       $query = $this->db->query("SELECT DISTINCT po.intid_po, po.datetgl, po_pusat_detail.*,po.intid_cabang, cabang.strnama_cabang, barang.strnama
		FROM po_pusat as po
		INNER JOIN po_pusat_detail ON po.intid_po = po_pusat_detail.intid_po
		INNER JOIN barang ON po_pusat_detail.intid_barang = barang.intid_barang
		INNER JOIN cabang ON po.intid_cabang = cabang.intid_cabang
		where po.intid_po = $id
		ORDER BY po.intid_po ASC");
        return $query->result();
	}
	
	
	   


	
function get_list_data_sj_filter($limit = 10, $offset = 0, $nm_cabang, $no)
  	{
        if($offset==""){ $offset=0; }		
	
		$query = $this->db->query("SELECT a.*, cabang.strnama_cabang FROM po a INNER JOIN cabang ON a.intid_cabang = cabang.intid_cabang where a.no_spkb <> '' and a.intid_cabang = '$nm_cabang' and a.intid_po = $no ORDER BY a.intid_po ASC  LIMIT $offset,$limit");
		
        return $query->result();
	}
	
	function get_list_data_detail_sj($id)
  	{
       $query = $this->db->query("SELECT DISTINCT po.intid_sj, po.datetgl, po.no_surat,
	   surat_jalan_detail.*, po.intid_cabang, cabang.strnama_cabang, 
	   barang.strnama
		FROM surat_jalan as po
		INNER JOIN surat_jalan_detail ON po.intid_sj = surat_jalan_detail.intid_sj
		INNER JOIN barang ON surat_jalan_detail.intid_barang = barang.intid_barang
		INNER JOIN cabang ON po.intid_cabang = cabang.intid_cabang
		where po.intid_sj = $id
		ORDER BY po.intid_sj ASC");
        return $query->result();
	}
	
		

	function get_MaxRetur() {
        $this->db->select_max('intid_retur');
		//$this->db->where('intid_jpenjualan = 9');
        $query = $this->db->get('retur');
        return $query;
     }
	 
	 	

		function insert_surat_sttb($data){
         	$tgl = date("Y-m-d");
       
            $data = array(
			'no_surat' => $this->input->post('no_surat'),
            'intid_cabang' => $this->input->post('intid_cabang'),
			'intid_week' => $this->input->post('intid_week'),
            'datetgl' => $tgl
            );
        $this->db->insert('sttb', $data);
        return $this->db->insert_id();
	}
 function get_cetak_surat_sttb($idPo) {
        $query = $this->db->query("SELECT DISTINCT sttb.intid_sttb, sttb.datetgl, sttb_detail.*,sttb.intid_cabang,sttb.no_surat, cabang.strnama_cabang, barang.strnama, sttb.intid_week
FROM sttb
INNER JOIN sttb_detail ON sttb.intid_sttb = sttb_detail.intid_sttb
INNER JOIN barang ON sttb_detail.intid_barang = barang.intid_barang
INNER JOIN cabang ON sttb.intid_cabang = cabang.intid_cabang
                                    WHERE sttb.intid_sttb = '$idPo'");

        return $query->result();
     }
	function get_list_data_sttb($limit = 10, $offset = 0)
  	{
        if($offset==""){ $offset=0; }		
	
		$query = $this->db->query("SELECT a.*, cabang.strnama_cabang FROM sttb a INNER JOIN cabang ON a.intid_cabang = cabang.intid_cabang ORDER BY a.intid_sttb ASC  LIMIT $offset,$limit");
        return $query->result();
	}
	
	
	function cek_sj($id){
       $query = $this->db->query("select intid_sj from surat_jalan where no_spkb = '$id'");
	   return $query->row();
	 
    }
	
   
	function update_stat_brg($where,$data) {
		if(is_array($where)):
			foreach ($where as $field=>$key):
				$this->db->where($field,$key);
			endforeach;
		endif;
		return $this->db->update('po_detail',$data);
	}

 function get_po_by_detailid($id)
	{
		$this->db->select('*');
		$this->db->where('intid_po', $id);
		return $this->db->get('po');
	}
	
	
	
	    function update_fisik($no_spkb, $po){
        
		       $data = array(
            		'no_spkb' => $no_spkb
				);
        $this->db->where('intid_po', $po);
		$this->db->update('po', $data);   //tambahn , $data

    }
	
	function getLap_Stok_opname($no_po, $intid_cabang){

	   $query = $this->db->query("SELECT barang.strnama,barang.intid_barang, po_detail.*
	
		FROM po_detail
		
		inner join po on po_detail.intid_po = po.intid_po
		INNER JOIN cabang ON cabang.intid_cabang = po.intid_cabang

		inner join barang on barang.intid_barang = po_detail.intid_barang
		WHERE po.intid_cabang = '$intid_cabang' and po.intid_po='$no_po'
		ORDER BY po_detail.intid_barang");
	   return $query->result();
    }
	
     function get_Cabang_PO($idPo) {
        $query = $this->db->query("SELECT po.intid_cabang FROM po
                                    WHERE po.intid_po = '$idPo'");

        return $query->row();
     }